package org.chen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.chen.domain.CrawlerTask;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
@Repository
public interface CrawlerTaskRepository extends BaseMapper<CrawlerTask> {
    
    @Select("SELECT source, COUNT(*) as count FROM crawler_task GROUP BY source")
    List<Map<String, Object>> groupBySource();
    
    default Map<String, Long> countBySource() {
        return groupBySource().stream()
                .collect(Collectors.toMap(
                        m -> (String) m.get("source"),
                        m -> ((Number) m.get("count")).longValue()
                ));
    }
}